Implementing Unit Tests | Pytest

Pytest is the built-in library provided by Python for testing in Python. It simplifies the process of writing and executing tests. Pytest supports unittest test cases execution. It has benefits like supporting built in assert statement, filtering of test cases, returning from last failing test etc.

We can install Pytest by writing the following command in the command prompt:

pip install pytest

Let’s start with a simple example. Here, we will make a file named as math_functions.py.

Python
# math_functions.py
def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

Now, we will make another file named as test_math_functions.py. We are making this file to perform testing using pytest using sample inputs.

Python
# test_math_functions.py
import math_functions

def test_add():
    assert math_functions.add(2, 3) == 5
    assert math_functions.add(-1, 1) == 0
    assert math_functions.add(0, 0) == 0

def test_subtract():
    assert math_functions.subtract(5, 3) == 2
    assert math_functions.subtract(0, 0) == 0
    assert math_functions.subtract(-1, -1) == 0

To run these tests using pytest, navigate to the directory containing these files in your terminal and run:

pytest

Output:

Getting Started With Unit Testing in Python

In Python, unit tests are the segments of codes that are written to test other modules and files that we refer to as a unit. Python Unit Testing is a very important part of the software development process that helps us to ensure that the code works properly without any errors. In this article, we will learn about Python Testing with the help of examples and proper explanations.

Table of Content

  • What is Python Unit Testing?
  • The Assert Statement
  • Implementing Unit Tests | Unittest
  • Implementing Unit Tests | Pytest
  • Implementing Unit Tests | Nose
  • Implementing Unit Tests | Doctest
  • How to Write Assertions in Python Testing
  • More Advanced Testing Scenarios
  • Automating the Execution of Tests

Similar Reads

What is Python Unit Testing?

Python Unit testing is a software testing technique where individual units or components of a software application are tested in isolation from the rest of the system. The primary goal of Python unit testing is checking the validation that each unit of the software performs as designed, according to its specifications. Each unit test looks at a small part of the code, making sure it works even when things get a little tricky....

The Assert Statement

Before diving to the testing framework, Let’s understand the assert statement, It is commonly used in Python unit testing frameworks like unittest and pytest to verify the correctness of code during testing. The assert statement in Python is used to check whether a given condition is true or false. It’s primarily used for debugging and testing purposes to verify assumptions made by the programmer during the development of the code....

Implementing Unit Tests | unittest

The unittest is built into the standard Python library used for testing with Python.  import unittest should be the starting line of code for using it. Depends upon the python version, it should differ as later versions of Python supports unittest and earlier versions supported unittest2....

Implementing Unit Tests | Pytest

Pytest is the built-in library provided by Python for testing in Python. It simplifies the process of writing and executing tests. Pytest supports unittest test cases execution. It has benefits like supporting built in assert statement, filtering of test cases, returning from last failing test etc....

Implementing Unit Tests | Nose

“Nose” is a popular testing framework in Python, primarily used for writing unit tests. Nose is a test runner that extends the capabilities of Python’s built-in unittest module. It provides a simpler syntax for writing tests and offers additional features for test discovery, parallel test execution, and plugin support....

Implementing Unit Tests | Doctest

Doctest Module is used to write tests in the form of interactive Python sessions within docstrings and then automatically run those tests to verify that the code behaves as expected. Doctest Module finds patterns in the docstring that looks like interactive shell commands....

How to Write Assertions in Python Testing

Assertion is nothing but validating the output against a known response. i.e. in above code, we have passed the list containing 3 values namely 10, 20 and 30, and we know that its multiplication result is 6000. So as a last step in code, we will be writing assertion code and above code comes up with assertEqual and surely it will give the output as 6000 and hence the testcase passes....

More Advanced Testing Scenarios

Fixture is used which is nothing but the data that is created as an input and reusing them.Parameterization can be followed which is nothing but running the same test several times by passing different values and expecting the same result.Need to cover different scenarios like handling expected failures, writing integration tests, testing in multiple environments etc....

Automating the Execution of Tests

Continuous Integration/Continuous Deployment tools are available. They help to run tests, compile, publish and also deploy into production.https://travis-ci.com/ is one among them which works well with Python and it is an open source project. Login into the site and create.travis.yml with the below contents:...